<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree :data="list" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.pid !== '' && data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit ? '编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hDialogClose"
      >
        <addor-edit
          v-if="showDialog"
          :id="curId"
          ref="refDeptDialog"
          :is-edit="isEdit"
          :origin-list="originList"
          @close="hClose"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/tranListToTreeData'
import AddorEdit from './deptDialog.vue'
export default {
  name: '',
  components: { AddorEdit },
  data() {
    return {
      list: [],
      showDialog: false,
      isEdit: false,
      curId: '',
      originList: []
    }
  },
  computed: {},
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const { data: res } = await getDepartments()
        const company = res.data.depts.shift()
        console.log('loadDepartments', company)
        this.originList = res.data.depts.map(({ id, name, pid, code }) => {
          return { id, name, code, pid }
        })
        this.list = tranListToTreeData(res.data.depts)
      } catch (err) {
        console.log(err)
      }
    },
    async doDel(id) {
      try {
        const { data: res } = await delDepartment(id)
        console.log('删除部门', res)
        this.$message.success(res.message)
      } catch (err) {
        this.$message({ type: 'error', message: '删除失败' })
        console.log(err)
      }
    },

    hAdd(id) {
      this.isEdit = false
      this.showDialog = true
      this.curId = id
    },
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.refDeptDialog.loadDetail()
      })
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
        })
        .catch(() => {})
    },
    hClose() {
      this.showDialog = false
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    hDialogClose() {
      console.log('close')
      this.$refs.refDeptDialog.resetForm()
    }
  }
}
</script>
<style lang='less'  scoped>
</style>
